package com.hy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.common.PageResult;
import com.hy.common.QueryPageBean;
import com.hy.common.Result;
import com.hy.mapper.DoctorsMapper;
import com.hy.mapper.PatienevaluationMapper;
import com.hy.mapper.PatientInfoMapper;
import com.hy.pojo.Doctors;
import com.hy.pojo.Patienevaluation;
import com.hy.pojo.PatientInfo;
import com.hy.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientInfoServiceImpl implements PatientService {
    @Autowired
    private PatientInfoMapper patientInfoMapper;
    @Autowired
    private DoctorsMapper doctorsMapper;
    @Autowired
    private PatienevaluationMapper patienevaluationMapper;
    @Override
    public PageResult getPatient(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<PatientInfo> patientInfoList=patientInfoMapper.getPatient(queryPageBean.getQueryString());
        PageInfo<PatientInfo>pageInfo=new PageInfo<>(patientInfoList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public Result updatePatient(PatientInfo patientInfo) {
        patientInfoMapper.updatePatient(patientInfo);
        return new Result(true,"更新成功");
    }

    @Override
    public Result getPatientid(Integer id) {
        PatientInfo patientInfo=patientInfoMapper.getPatientid(id);
        return new Result(true,"查询成功",patientInfo);
    }

    @Override
    public List<Doctors> getDoctors() {
        List<Doctors>doctorsList=doctorsMapper.getDoctors();
        return doctorsList;
    }

    @Override
    public Result addPatient(PatientInfo patientInfo) {
        Doctors doctorXx = doctorsMapper.getDoctorXxByid(patientInfo.getDoctorid());
        doctorXx.setIsVerified(doctorXx.getIsVerified()+1);
        doctorsMapper.updateDoctors(doctorXx);
        patientInfoMapper.addPatient(patientInfo);
        return new Result(true,"添加成功");
    }

    @Override
    public Result deletePatient(Integer id) {
//        PatientInfo patientid = patientInfoMapper.getPatientid(id);
//        Doctors doctorXx = doctorsMapper.getDoctorXx(patientid.getDoctorid());
//        doctorXx.setIsVerified(doctorXx.getIsVerified()-1);
//        doctorsMapper.updateDoctors(doctorXx);
//         patientInfoMapper.deletePatient(id);
//         return new Result(true,"删除成功");
        // 获取患者信息
        PatientInfo patientInfo = patientInfoMapper.getPatientByid(id);
        if (patientInfo == null) {
            return new Result(false, "患者信息不存在");
        }

        // 获取医生信息
        Integer doctorId = patientInfo.getDoctorid();
        if (doctorId == null) {
            return new Result(false, "患者未关联医生");
        }

        Doctors doctorXx = doctorsMapper.getDoctorXxByid(doctorId);
        if (doctorXx == null) {
            return new Result(false, "医生信息不存在");
        }

        // 更新医生的 isVerified 字段
        Integer isVerified = doctorXx.getIsVerified();
        if (isVerified == null) {
            isVerified = 0; // 如果 isVerified 为 null，设置默认值为 0
        }
        doctorXx.setIsVerified(isVerified - 1);
        doctorsMapper.updateDoctors(doctorXx);

        // 删除患者信息
        patientInfoMapper.deletePatient(id);

        return new Result(true, "删除成功");
    }

    @Override
    public Result feedback(Patienevaluation patienevaluation) {
        System.err.println(patienevaluation);
        Patienevaluation patienevaluation1= patienevaluationMapper.PatienevaluationById(patienevaluation.getDoctorId(),patienevaluation.getPatientId());
        patienevaluation.setId(patienevaluation1.getId());
        if(patienevaluation1!=null){
           patienevaluationMapper.updateEvaluation(patienevaluation);
           return new Result(true,"你已更改反馈内容");
        }
        patienevaluationMapper.addEvaluation(patienevaluation);
        return new Result(true,"反馈成功");
    }


}
